package org.zzk.dao;

import com.google.gson.JsonObject;
import lombok.Data;

@Data
public class GameResult {
    public enum ResultType {
        RED_WIN,    // 红方胜
        BLACK_WIN,  // 黑方胜
        DRAW,       // 和棋
        CONTINUE    // 未结束
    }

    private final ResultType result;
    private final String reason;
    private final ChessBoard finalBoard;

    public GameResult(ResultType result, String reason, ChessBoard finalBoard) {
        this.result = result;
        this.reason = reason;
        this.finalBoard = finalBoard;
    }

    // 静态工厂方法
    public static GameResult redWin(String reason, ChessBoard board) {
        return new GameResult(ResultType.RED_WIN, reason, board);
    }

    public static GameResult blackWin(String reason, ChessBoard board) {
        return new GameResult(ResultType.BLACK_WIN, reason, board);
    }

    public static GameResult draw(String reason, ChessBoard board) {
        return new GameResult(ResultType.DRAW, reason, board);
    }

    public static GameResult continueGame() {
        return new GameResult(ResultType.CONTINUE, "", null);
    }

    // 业务方法
    public boolean isGameOver() {
        return result != ResultType.CONTINUE;
    }

    public String getWinner() {
        String res = null;
        switch (result) {
            case RED_WIN:
                res = "red";
                break;
            case BLACK_WIN:
                res = "black";
                break;
            default:
                res = "draw";
                break;
        };
        return res;
    }

    public JsonObject toJson() {
        JsonObject json = new JsonObject();
        json.addProperty("result", result.name());
        json.addProperty("reason", reason);
        if (finalBoard != null) {
            json.add("final_board", finalBoard.toJson());
        }
        return json;
    }
}
